<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: AddUserCurrency.php
 * Date: 2017/1/25
 * Time: 3:06
 */

namespace app\api\model\page\user;

use app\api\model\dao\UserCurrency;
use app\api\model\data\currency\ConsumeCurrency;
use app\library\Error;
use app\library\PageBase;
use think\Log;
use think\Validate;


class AddUserCurrency extends PageBase
{
    public function __construt()
    {
        parent::__construct();
    }

    public function excute($param)
    {
        $ret = $this->check($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }
        unset($ret);
        $obj = new ConsumeCurrency();
        $ret = $obj->addCurrency($param['user_id'], $param['amount'], $param['type'], '1');
        if (false === $ret) {
            return $this->getRes(Error::ERR_SYS);
        }
        /*
        $obj = new UserCurrency();
        try {
            $obj->changeCurrency($param['user_id'], $param['type'], $param['amount']);
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return $this->getRes(Error::ERR_SYS);
        }
        */
        return $this->getRes();
    }

    public function check($param)
    {
        $rule = [
            'user_id' => 'require',
            'type' => 'require|In:1,2',
            'amount' => 'require|integer|gt:0'
        ];
        $msg = [
            'user_id.require' => 'user_id必须',
            'type.require' => 'type必须',
            'type.In' => '类型错误',
            'amount.require' => 'amount必须',
            'amount.integer' => 'amount必须正整数',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }
}
